home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / CACHEMANAGER.PY < prev    next >
Encoding:
Text File  |  2000-05-23  |  11.2 KB  |  314 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__='''Cache management support
  65.  
  66.  
  67. $Id: CacheManager.py,v 1.18 2000/05/23 15:38:43 brian Exp $'''
  68. __version__='$Revision: 1.18 $'[11:-2]
  69.  
  70. import Globals, time, sys
  71.  
  72. class CacheManager:
  73.     """Cache management mix-in
  74.     """
  75.     _cache_age=60
  76.     _cache_size=400
  77.     _vcache_age=60
  78.     _vcache_size=400
  79.  
  80.     manage_cacheParameters=Globals.HTMLFile('cacheParameters', globals())
  81.     manage_cacheGC=Globals.HTMLFile('cacheGC', globals())
  82.  
  83.     def cache_length(self):
  84.         try: db=self._p_jar.db()
  85.         except:
  86.             # BoboPOS2
  87.             return len(Globals.Bobobase._jar.cache)
  88.         else: return db.cacheSize()
  89.  
  90.     def database_size(self):
  91.         try: db=self._p_jar.db()
  92.         except:
  93.             # BoboPOS2
  94.             return len(Globals.Bobobase._jar.db.index)*4
  95.         else: return db.objectCount()
  96.  
  97.     def cache_age(self):
  98.         try:
  99.             if self._p_jar.getVersion():
  100.                 return self._vcache_age
  101.         except: pass
  102.  
  103.         return self._cache_age
  104.  
  105.     def manage_cache_age(self,value,REQUEST):
  106.         "set cache age"
  107.         try:
  108.             v=self._p_jar.getVersion()
  109.         except:
  110.             # BoboPOS2:
  111.             if self._p_jar.db is not Globals.Bobobase._jar.db:
  112.                 raise 'Version Error', (
  113.                     '''You may not change the database cache age
  114.                     while working in a <em>version</em>''')
  115.             self._cache_age=Globals.Bobobase._jar.cache.cache_age=value
  116.         else:
  117.             if v:
  118.                 self._vcache_age=value
  119.                 self._p_jar.db().setVersionCacheDeactivateAfter(value)
  120.             else:
  121.                 self._cache_age=value
  122.                 self._p_jar.db().setCacheDeactivateAfter(value)
  123.  
  124.         if REQUEST is not None:
  125.             response=REQUEST['RESPONSE']
  126.             response.redirect(REQUEST['URL1']+'/manage_cacheParameters')
  127.  
  128.  
  129.  
  130.     def cache_size(self):
  131.         try:
  132.             if self._p_jar.getVersion():
  133.                 return self._vcache_size
  134.         except: pass
  135.         return self._cache_size
  136.  
  137.     def manage_cache_size(self,value,REQUEST):
  138.         "set cache size"
  139.         try:
  140.             v=self._p_jar.getVersion()
  141.         except: 
  142.             # BoboPOS2:
  143.             if self._p_jar.db is not Globals.Bobobase._jar.db:
  144.                 raise 'Version Error', (
  145.                     '''You may not change the database cache size
  146.                     while working in a <em>version</em>''')
  147.             self._cache_size=Globals.Bobobase._jar.cache.cache_size=value
  148.         else:
  149.             if v:
  150.                 self._vcache_size=value
  151.                 self._p_jar.db().setVersionCacheSize(value)
  152.             else:
  153.                 self._cache_size=value
  154.                 self._p_jar.db().setCacheSize(value)
  155.  
  156.         if REQUEST is not None:
  157.             response=REQUEST['RESPONSE']
  158.             response.redirect(REQUEST['URL1']+'/manage_cacheParameters')
  159.  
  160.  
  161.     def cacheStatistics(self):
  162.         try: return self._p_jar.db().cacheStatistics()
  163.         except: pass
  164.  
  165.         # BoboPOS 2
  166.         return (
  167.             ('Mean time since last access (minutes)',
  168.              "%.4g" % (Globals.Bobobase._jar.cache.cache_mean_age/60.0)),
  169.             ('Deallocation rate (objects/minute)',
  170.              "%.4g" % (Globals.Bobobase._jar.cache.cache_mean_deal*60)),
  171.             ('Deactivation rate (objects/minute)',
  172.              "%.4g" % (Globals.Bobobase._jar.cache.cache_mean_deac*60)),
  173.             ('Time of last cache garbage collection',
  174.              time.asctime(time.localtime(
  175.                  Globals.Bobobase._jar.cache.cache_last_gc_time
  176.                  ))
  177.              ),
  178.             )
  179.         
  180.  
  181.     # BoboPOS 2
  182.     def cache_mean_age(self):
  183.         return Globals.Bobobase._jar.cache.cache_mean_age/60.0
  184.  
  185.     # BoboPOS 2
  186.     def cache_mean_deal(self):
  187.         return Globals.Bobobase._jar.cache.cache_mean_deal*60
  188.  
  189.     # BoboPOS 2
  190.     def cache_mean_deac(self):
  191.         return Globals.Bobobase._jar.cache.cache_mean_deac*60
  192.  
  193.     # BoboPOS 2
  194.     def cache_last_gc_time(self):
  195.         t=Globals.Bobobase._jar.cache.cache_last_gc_time
  196.         return time.asctime(time.localtime(t))
  197.  
  198.     def manage_full_sweep(self,value,REQUEST):
  199.         "Perform a full sweep through the cache"
  200.         try: db=self._p_jar.db()
  201.         except:
  202.             # BoboPOS2
  203.             Globals.Bobobase._jar.cache.full_sweep(value)
  204.         else: db.cacheFullSweep(value)
  205.  
  206.         if REQUEST is not None:
  207.             response=REQUEST['RESPONSE']
  208.             response.redirect(REQUEST['URL1']+'/manage_cacheGC')
  209.  
  210.     def manage_minimize(self,value,REQUEST):
  211.         "Perform a full sweep through the cache"
  212.         try: db=self._p_jar.db()
  213.         except:
  214.             # BoboPOS2
  215.             Globals.Bobobase._jar.cache.minimize(value)
  216.         else: db.cacheMinimize(value)
  217.  
  218.         if REQUEST is not None:
  219.             response=REQUEST['RESPONSE']
  220.             response.redirect(REQUEST['URL1']+'/manage_cacheGC')
  221.  
  222.     def initialize_cache(self):
  223.         try: db=self._p_jar.db()
  224.         except:
  225.             # BoboPOS2
  226.             Globals.Bobobase._jar.cache.cache_size=self._cache_size
  227.             Globals.Bobobase._jar.cache.cache_age =self._cache_age
  228.         else:
  229.             db.setCacheSize(self._cache_size)
  230.             db.setCacheDeactivateAfter(self._cache_age)
  231.             db.setVersionCacheSize(self._vcache_size)
  232.             db.setVersionCacheDeactivateAfter(self._vcache_age)
  233.  
  234.     def cache_detail(self):
  235.         try: db=self._p_jar.db()
  236.         except:
  237.             # BoboPOS2            
  238.             detail={}
  239.             for oid, ob in Globals.Bobobase._jar.cache.items():
  240.                 if hasattr(ob, '__class__'):
  241.                     ob=ob.__class__
  242.                     decor=''
  243.                 else: decor=' class'
  244.                 c="%s.%s%s" % (ob.__module__ or '', ob.__name__, decor)
  245.                 if detail.has_key(c): detail[c]=detail[c]+1
  246.                 else: detail[c]=1
  247.             detail=detail.items()
  248.         else:
  249.             # ZODB 3
  250.             detail=db.cacheDetail()
  251.             detail=map(lambda d:
  252.                        (("%s.%s" % (d[0].__module__, d[0].__name__)), d[1]),
  253.                        detail.items())
  254.  
  255.         detail.sort()
  256.         return detail
  257.  
  258.     def cache_extreme_detail(self):
  259.         try: db=self._p_jar.db()
  260.         except:
  261.             # BoboPOS2            
  262.             detail=[]
  263.             rc=sys.getrefcount
  264.             db=Globals.Bobobase._jar.db
  265.             for oid, ob in Globals.Bobobase._jar.cache.items():
  266.                 id=oid
  267.  
  268.                 if hasattr(ob, '__class__'):
  269.                     if hasattr(ob,'__dict__'):
  270.                         d=ob.__dict__
  271.                         if d.has_key('id'):
  272.                             id="%s (%s)" % (oid, d['id'])
  273.                         elif d.has_key('__name__'):
  274.                             id="%s (%s)" % (oid, d['__name__'])
  275.                     ob=ob.__class__
  276.                     decor=''
  277.  
  278.                 else: decor=' class'
  279.  
  280.                 detail.append({
  281.                     'oid': id,
  282.                     'klass': "%s.%s%s" % (ob.__module__, ob.__name__, decor),
  283.                     'rc': rc(ob)-4,
  284.                     'references': db.objectReferencesIn(oid),
  285.                     })
  286.             return detail
  287.         else:
  288.             # ZODB 3
  289.             return db.cacheExtremeDetail()
  290.  
  291. Globals.default__class_init__(CacheManager)
  292.  
  293.